package cn.com.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.opensymphony.xwork2.ActionSupport;

import cn.com.biz.DoctorBiz;

import cn.com.entity.Doctor;


public class DoctorAction extends ActionSupport implements SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DoctorBiz doctorbiz;
	private String doctormsg;
	private Doctor doctor;
	private Map<String, Object> session;
	
	public DoctorBiz getDoctorbiz() {
		return doctorbiz;
	}

	public void setDoctorbiz(DoctorBiz doctorbiz) {
		this.doctorbiz = doctorbiz;
	}

	public String getDoctormsg() {
		return doctormsg;
	}

	public void setDoctormsg(String doctormsg) {
		this.doctormsg = doctormsg;
	}

	public Doctor getDoctor() {
		return doctor;
	}

	public void setDoctor(Doctor doctor) {
		this.doctor = doctor;
	}

	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> arg0) {
		session=arg0;
	}
	
	//医生登录
	public String doctorLogin(){
		@SuppressWarnings("resource")
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		doctorbiz=(DoctorBiz)ac.getBean("doctorbiz");
		Doctor d=doctorbiz.doctorLogin(doctor);
		if(d!=null&&d.getDepartmentsid()==3){
			session.put("doctor", d);
			return "pexaminationdoctor";
		}else if(d!=null&&d.getDepartmentsid()!=3){
			session.put("doctor", d);
			return "doctor";
		}
		else{
			setDoctormsg("登录失败，请重新登录");
			return INPUT;
		}
	}
	//查询所有医生
	public String selectAllDoctor(){
		@SuppressWarnings("resource")
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		doctorbiz=(DoctorBiz)ac.getBean("doctorbiz");
		List<Doctor> list=doctorbiz.selectAllNews();
		session.put("alldoctorlist", list);
		return SUCCESS;
	}
	
	//查询一个医生
	public String selectOneDoctor(){
		@SuppressWarnings("resource")
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		doctorbiz=(DoctorBiz)ac.getBean("doctorbiz");
		Doctor d=doctorbiz.selectOneDoctor(doctor);
		session.put("onedoctorlist", d);
		return SUCCESS;
	}
	//添加医生
	public String addDoctor(){
		@SuppressWarnings("resource")
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		doctorbiz=(DoctorBiz)ac.getBean("doctorbiz");
		boolean  bool=doctorbiz.addDoctor(doctor);
		if(bool){
			setDoctormsg("添加医生成功");
			return SUCCESS;
		}else{
			setDoctormsg("添加医生失败，请重新添加");
			return ERROR; 
		}
	}
	//更新医生
	public String updateDoctor(){
		@SuppressWarnings("resource")
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		doctorbiz=(DoctorBiz)ac.getBean("doctorbiz");
		boolean  bool=doctorbiz.updateDoctor(doctor);
		if(bool){
			setDoctormsg("更新医生成功");
			return SUCCESS;
		}else{
			setDoctormsg("更新医生失败，请重新更新");
			return ERROR; 
		}
	}
	//删除医生
	public String deleteDoctor(){
		@SuppressWarnings("resource")
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		doctorbiz=(DoctorBiz)ac.getBean("doctorbiz");
		boolean  bool=doctorbiz.deleteDoctor(doctor);
		if(bool){
			setDoctormsg("删除医生成功");
			return SUCCESS;
		}else{
			setDoctormsg("删除医生失败，请重新删除");
			return ERROR; 
		}
	}
	
	
	
}
